<template>
  <div class="SalSobCfg">
    <div class="sal_sob_main">
      <el-table :data="emps" style="width: 100%" stripe border>
        <el-table-column type="selection" width="55"> </el-table-column>
        <el-table-column prop="name" label="姓名" width="80"> </el-table-column>
        <el-table-column prop="workID" label="工号" width="120">
        </el-table-column>
        <el-table-column prop="email" label="邮箱地址" width="200">
        </el-table-column>
        <el-table-column prop="phone" label="电话号码" width="130">
        </el-table-column>
        <el-table-column prop="department.name" label="所属部门" width="120">
        </el-table-column>
        <el-table-column label="工资账套" width="260">
          <template slot-scope="scope">
            <el-tooltip placement="right" v-if="scope.row.salary">
              <div slot="content">
                <table>
                  <tr>
                    <td>基本工资：</td>
                    <td>{{ scope.row.salary.basicSalary }}</td>
                  </tr>
                  <tr>
                    <td>午餐补助：</td>
                    <td>{{ scope.row.salary.lunchSalary }}</td>
                  </tr>
                  <tr>
                    <td>交通补助：</td>
                    <td>{{ scope.row.salary.trafficSalary }}</td>
                  </tr>
                  <tr>
                    <td>奖金：</td>
                    <td>{{ scope.row.salary.bonus }}</td>
                  </tr>
                  <tr>
                    <td>养老金基数：</td>
                    <td>{{ scope.row.salary.pensionBase }}</td>
                  </tr>
                  <tr>
                    <td>养老金比率：</td>
                    <td>{{ scope.row.salary.pensionPer }}</td>
                  </tr>
                  <tr>
                    <td>医疗基数：</td>
                    <td>{{ scope.row.salary.medicalBase }}</td>
                  </tr>
                  <tr>
                    <td>医疗比率：</td>
                    <td>{{ scope.row.salary.medicalPer }}</td>
                  </tr>
                  <tr>
                    <td>公积金基数：</td>
                    <td>{{ scope.row.salary.accumulationFundBase }}</td>
                  </tr>
                  <tr>
                    <td>公积金比率：</td>
                    <td>{{ scope.row.salary.accumulationFundPer }}</td>
                  </tr>
                </table>
              </div>
              <el-tag size="mini">{{ scope.row.salary.name }}</el-tag>
            </el-tooltip>
            <el-tag size="mini" v-else>暂未设置账套</el-tag>
          </template>
        </el-table-column> 
         <el-table-column label="操作">
          <template slot-scope="scope">
            <el-popover placement="bottom" title="修改工资账套" width="200" trigger="click" @show="showUpdateView(scope.row.salary)"
              @hide="hideUpdateView(scope.row)">
              <el-select v-model="currentSalary" placeholder="请选择">
                <el-option v-for="item in salaries" :key="item.id" :label="item.name" :value="item.id">
                </el-option>
              </el-select>
              <el-button slot="reference" type="danger" size="mini">
                修改工资账套
              </el-button>
            </el-popover>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <!-- 分页 -->
    <div class="sal_sob_footer">
      <el-pagination background 
      @size-change="handleSizeChange" 
      @current-change="handleCurrentChange"
        :current-page="currentPage" :page-sizes="[10, 20, 30, 40]" :page-size="size"
        layout="total, sizes, prev, pager, next, jumper" :total="total">
      </el-pagination>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      // 员工账套
      emps: [],
      // 工资账套
      salaries: [],
      // 当前页
      currentPage: 1,
      // 每页显示多少条数据
      size: 10,
      // 总条数
      total: 0,
      // 当前工资账套
      currentSalary: null,
      }
  },
  methods: {
    hideUpdateView(data) {
      if (this.currentSalary && this.currentSalary != data.salary.id) {

        this.putRequest('/salary/sobcfg/?eid=' + data.id + '&sid=' + this.currentSalary).then(resp => {
          if (resp) {
            this.initEmps();
          }
        })

      }  
    },
    // 处理修改事件
    showUpdateView(data) {
      if (data) {
        this.currentSalary = data.id;
      } 
    },
    // 上一页下一夜
    handleCurrentChange(page) {
      this.currentPage = page;
      this.initEmps();
    },
    // 条数发送改变
    handleSizeChange(page) {
      this.size = page;
      this.initEmps();
    },
    // 初始化所有员工工资账套
    initEmps() {
      this.getRequest('/salary/sobcfg/?currentPage='+this.currentPage + '&size=' + this.size).then((resp) => {
        if (resp) {
          this.emps = resp.data;
          this.total = resp.total;
        }
      });
    },
    // 初始化工资账套
    initSalaries() {
      this.getRequest('/salary/sobcfg/salaries').then((resp) => {
        if (resp) {
          this.salaries = resp;
        }
      });
    },
  },
  created() {
    this.initEmps();
    this.initSalaries();
  }
}
</script>

<style>
.sal_sob_main {
  margin: 10px 0;
}

.sal_sob_footer {
  display: flex;
  justify-content: flex-end;
}
</style>